package com.aisupport.deviceidtest;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button btnGet, btnCheck;
    EditText etIMEI;
    TextView tvMAC,tvSerialNumber, tvAndroidID,etIMEICheck;

    Activity mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_main);

        Log.d("airain", "onCreate: fwind");
        btnGet = (Button) findViewById(R.id.btnGet);
        btnCheck = (Button) findViewById(R.id.btnCheck);

        tvMAC = (TextView) findViewById(R.id.tvMAC);
        etIMEI = (EditText) findViewById(R.id.etIMEI);
        tvSerialNumber = (TextView) findViewById(R.id.etSerialNumber);
        tvAndroidID = (TextView) findViewById(R.id.etAndroidID);

        etIMEICheck = (TextView) findViewById(R.id.etIMEICheck);

        btnGet.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                refreshInfo();
            }
        });
        btnCheck.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                final String aiIMEI = etIMEI.getText().toString();
                final String imeiCheck = DeviceIdUtil.isValidIMEI(aiIMEI) ? "ok" : "error";
                runOnUiThread(new Runnable() {
                    public void run() {
                        if (aiIMEI.length() > 14) {
                            etIMEICheck.setText(imeiCheck + " " + DeviceIdUtil.getCheckSum(aiIMEI.substring(0, 14)));
                        } else {
                            etIMEICheck.setText(imeiCheck + " length = " + aiIMEI.length());
                        }
                    }
                });

            }
        });

        refreshInfo();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    String lastCheck;

    void refreshInfo() {

        final String aiIMEI = (null == ((TelephonyManager) mContext.getSystemService(TELEPHONY_SERVICE))) ? "can not get imei" : ((TelephonyManager) mContext.getSystemService(TELEPHONY_SERVICE)).getDeviceId();
        Log.d("airain", "refreshInfo: ");
        WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
        String macAddress = "";
        if (null != info) {
            macAddress = info.getMacAddress();
        }
        final String aiMAC = macAddress;
        final boolean imeiCheck = DeviceIdUtil.isValidIMEI(aiIMEI);
        if (aiIMEI.length() > 14) {
            lastCheck = DeviceIdUtil.getCheckSum(aiIMEI.substring(0, 14));
        } else {
            lastCheck = "imei = " + aiIMEI + "  length = " + aiIMEI.length();
        }

        final String serialNumber = DeviceIdUtil.getSerialNumber();
        final String androidId = DeviceIdUtil.getAndroidID(MainActivity.this);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvMAC.setText(aiMAC);
                etIMEI.setText(aiIMEI);

                tvSerialNumber.setText(serialNumber);
                tvAndroidID.setText(androidId);

                String checkStr = imeiCheck ? "ok" : "error " + lastCheck;
                etIMEICheck.setText(checkStr);
            }
        });

    }

}
